package at.fhj.ase.business;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.stream.XMLStreamReader;

import at.fhj.ase.data.AttackOutput;
import at.fhj.ase.data.Filter;
import at.fhj.ase.data.LogLineInformation;

/**
 * 
 * @author Zandmiralvand, Mansouri, Jaklitsch
 * 
 * This class provides methods for comparing input data to the profile in order
 * to detect anomalies that are contained in the input data.
 *
 */

public class AttackDetector {
	///////////////We should use it(the logLine)later for comparison line by line//////////////////
	private String logLine;
	private List<Filter> attackFilters;


	////////////////Constructor for initialization of attackFilters////////////
	public AttackDetector(List<Filter> attackFilters){
		setAttackFilters(attackFilters);
	}
	///////////////////Setting Method for AttackDetector /////////////////////
	public void setAttackFilters(List<Filter> attackFilters) {
		if(attackFilters==null || attackFilters.size()== 0){
			throw new IllegalArgumentException("AttackFilters may not be null or empty");
		}
		this.attackFilters = attackFilters;
	}

	//////////READ_ACCESS_LOF_FILE//////////
	int counter=0;
	int lineCounter=0;
	public List<AttackOutput> readAccessLogFile(String fileName) throws IOException{
		File file = new File(fileName);

		FileInputStream fileStream = new FileInputStream(file);

		// Get the object of DataInputStream
		DataInputStream input = new DataInputStream(fileStream);
		BufferedReader bufferReader = new BufferedReader(new InputStreamReader(input));

		List<AttackOutput> detectedAttacks = new ArrayList<AttackOutput>();

		//Read File Line By Line
		while ((logLine = bufferReader.readLine()) != null){
			++lineCounter;
			//System.out.println("LogLine Number: "+ lineCounter);
			for(Filter f:this.attackFilters)	{
				//Pattern pattern=Pattern.compile("[<>]");
				System.out.println("obtaining matcher for pattern: " + f.getFilterRule().pattern());
				Matcher matcher = f.getFilterRule().matcher(logLine);

				//			Matcher matcher= pattern.matcher(logLine);

				if(matcher.find()){
					System.out.println("Attack Detected["+ ++counter + "] !!!" + "\n");	    
					//instantiate a LogLine object
					LogLineInformation logDetails=new LogLineInformation(logLine, lineCounter, file);
					detectedAttacks.add(new AttackOutput(f.getTags(),f.getDescription(),logDetails));				  
				}
				else{
					System.out.println(logLine);
					System.out.println("It could not find any attack!" + "\n");
				}

			}
		}
		//Close the input stream
		try {
			input.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return detectedAttacks;
	}
	
	
	////////////////Automatically Generated/////////////
	public AttackDetector(List<Filter> attackFilters, String accessLogFileName) throws FileNotFoundException, IOException{
		this.attackFilters = attackFilters;
		new FileInputStream(accessLogFileName);
	}

	public Boolean accessLogIsValid(){
		//TODO: implement validation
		return Boolean.TRUE;
	}

	public void checkForAttacks(XMLStreamReader parser){
		if(accessLogIsValid()){

		}
	}

}
